package com.easy.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.auth.mapper.AdminMapper;
import com.easy.auth.util.PasswordDecode;
import com.easy.auth.util.TokenUtil;
import com.easy.common.bean.Admin;
import com.easy.common.exception.ServiceException;
import com.easy.common.service.IAdminService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@DubboService
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Override
    public String login(String username, String password) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Admin::getUsername, username);
        Admin admin = this.getOne(queryWrapper);
        if(admin==null){
            throw new ServiceException("登录账号不存在");
        }
        // 密码验证
        if(!PasswordDecode.verify(password,admin.getPassword())){
            HashMap<String,Object> map = new HashMap<>();
            map.put(TokenUtil.TYPE,TokenUtil.TYPE_ADMIN);
            map.put(TokenUtil.ADMINID,admin.getId());
            return TokenUtil.generateToken(map);
        }
        throw new ServiceException("密码错误");

    }
}
